********************************************************************************
** 	TITLE:		CA04_results                                                  ** 
**	AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       table12                                                       **
**	VERSION:	Stata 16					                                  **
**	DATE:		October 2022  				                                  **
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close
log using "CA04_results.smcl", replace

* Import the dataset

copy https://www.elections.ca/scripts/OVR2004/23/data/table12.csv table12.csv, replace
type table12.csv
import delimited table12.csv, clear

* Rename variables

rename district districtname04 
rename candidate candidate04 
rename residence residence04
rename occupation occupation04
rename numberofvotes numberofvotes04
rename numberofvotespercent numberofvotespercent04
rename numbermajority majoritynumber04
rename majoritypercent majoritypercent04

* Generate dichotomous variable for each party 

gen lib04 = strpos(candidate04, "Liberal/Libéral") > 0
gen con04 = strpos(candidate04, "Conservative/conservateur") > 0
gen ndp04 = strpos(candidate04, "N.D.P./N.P.D.") > 0
gen green04 = strpos(candidate04, "Green Party/Parti vert") > 0
gen bq04 = strpos(candidate04, "Bloc Québécois/Bloc Québécois") > 0

* Generate party variable

gen party04 = 1 if lib04 == 1
replace party04 = 2 if con04 == 1
replace party04 = 3 if ndp04 == 1
replace party04 = 4 if bq04 == 1
replace party04 = 5 if green04 == 1
replace party04 = 88 if party04 == . 

* Generate rank of parties

gsort districtname04 -numberofvotes04
bysort districtname04 : gen rank04 = _n

gen first04 = party04 if rank04 == 1
gen second04 = party04 if rank04 == 2
gen third04 = party04 if rank04 == 3

bysort districtname04 : gen winner04 = first04[1]
bysort districtname04 : gen runnerup04 = second04[2]
bysort districtname04 : gen thirdplace04 = third04[3]

* Generate margins of victory

bysort districtname04 : gen margin04 = numberofvotespercent04[1] - numberofvotespercent04[2]

* Generate effective number of electoral parties

gen sqrvotepr04 = (numberofvotespercent04/100)^2

bysort districtname04 : egen sumsqrvotepr04 = sum(sqrvotepr04)

gen enep04 = 1/sumsqrvotepr04

* Labelling parties

label define party 1 "Liberal Party" 2 "Conservative Party" 3 "New Democratic Party" 4 "Bloc Quebecois" 5 "Green Party" 88 "Other"
label values winner04 runnerup04 thirdplace04 party

* Generate district numbers

gen districtnumber04 = .

replace districtnumber04 = 10001 if districtname04 == "Avalon"
replace districtnumber04 = 10002 if districtname04 == "Bonavista--Exploits"
replace districtnumber04 = 10003 if districtname04 == "Humber--St. Barbe--Baie Verte"
replace districtnumber04 = 10004 if districtname04 == "Labrador"
replace districtnumber04 = 10005 if districtname04 == "Random--Burin--St. George's"
replace districtnumber04 = 10006 if districtname04 == "St. John's North/St. John's-Nord"
replace districtnumber04 = 10007 if districtname04 == "St. John's South/St. John's-Sud"
replace districtnumber04 = 11001 if districtname04 == "Cardigan"
replace districtnumber04 = 11002 if districtname04 == "Charlottetown"
replace districtnumber04 = 11003 if districtname04 == "Egmont"
replace districtnumber04 = 11004 if districtname04 == "Malpeque"
replace districtnumber04 = 12001 if districtname04 == "Cape Breton--Canso"
replace districtnumber04 = 12002 if districtname04 == "Central Nova/Nova-Centre"
replace districtnumber04 = 12003 if districtname04 == "Dartmouth--Cole Harbour"
replace districtnumber04 = 12004 if districtname04 == "Halifax"
replace districtnumber04 = 12005 if districtname04 == "Halifax West/Halifax-Ouest"
replace districtnumber04 = 12006 if districtname04 == "Kings--Hants"
replace districtnumber04 = 12007 if districtname04 == "North Nova/Nova-Nord"
replace districtnumber04 = 12008 if districtname04 == "Sackville--Eastern Shore"
replace districtnumber04 = 12009 if districtname04 == "South Shore--St. Margaret's"
replace districtnumber04 = 12010 if districtname04 == "Sydney--Victoria"
replace districtnumber04 = 12011 if districtname04 == "West Nova/Nova-Ouest"
replace districtnumber04 = 13001 if districtname04 == "Acadie--Bathurst"
replace districtnumber04 = 13002 if districtname04 == "Beauséjour"
replace districtnumber04 = 13003 if districtname04 == "Fredericton"
replace districtnumber04 = 13004 if districtname04 == "Fundy"
replace districtnumber04 = 13005 if districtname04 == "Madawaska--Restigouche"
replace districtnumber04 = 13006 if districtname04 == "Miramichi"
replace districtnumber04 = 13007 if districtname04 == "Moncton--Riverview--Dieppe"
replace districtnumber04 = 13008 if districtname04 == "St. Croix--Belleisle"
replace districtnumber04 = 13009 if districtname04 == "Saint John"
replace districtnumber04 = 13010 if districtname04 == "Tobique--Mactaquac"
replace districtnumber04 = 24001 if districtname04 == "Abitibi--Témiscamingue"
replace districtnumber04 = 24002 if districtname04 == "Ahuntsic"
replace districtnumber04 = 24003 if districtname04 == "Alfred-Pellan"
replace districtnumber04 = 24004 if districtname04 == "Argenteuil--Mirabel"
replace districtnumber04 = 24005 if districtname04 == "Beauce"
replace districtnumber04 = 24006 if districtname04 == "Beauharnois--Salaberry"
replace districtnumber04 = 24007 if districtname04 == "Beauport"
replace districtnumber04 = 24008 if districtname04 == "Berthier--Maskinongé"
replace districtnumber04 = 24009 if districtname04 == "Bourassa"
replace districtnumber04 = 24010 if districtname04 == "Brome--Missisquoi"
replace districtnumber04 = 24011 if districtname04 == "Brossard--La Prairie"
replace districtnumber04 = 24012 if districtname04 == "Chambly--Borduas"
replace districtnumber04 = 24013 if districtname04 == "Charlesbourg"
replace districtnumber04 = 24014 if districtname04 == "Charlevoix--Montmorency"
replace districtnumber04 = 24015 if districtname04 == "Châteauguay--Saint-Constant"
replace districtnumber04 = 24016 if districtname04 == "Chicoutimi--Le Fjord"
replace districtnumber04 = 24017 if districtname04 == "Compton--Stanstead"
replace districtnumber04 = 24018 if districtname04 == "Drummond"
replace districtnumber04 = 24019 if districtname04 == "Gaspésie--Îles-de-la-Madeleine"
replace districtnumber04 = 24020 if districtname04 == "Gatineau"
replace districtnumber04 = 24021 if districtname04 == "Hochelaga"
replace districtnumber04 = 24022 if districtname04 == "Honoré-Mercier"
replace districtnumber04 = 24023 if districtname04 == "Hull--Aylmer"
replace districtnumber04 = 24024 if districtname04 == "Jeanne-Le Ber"
replace districtnumber04 = 24025 if districtname04 == "Joliette"
replace districtnumber04 = 24026 if districtname04 == "Jonquière--Alma"
replace districtnumber04 = 24027 if districtname04 == "Lac-Saint-Louis"
replace districtnumber04 = 24028 if districtname04 == "La Pointe-de-l'Île"
replace districtnumber04 = 24029 if districtname04 == "LaSalle--Émard"
replace districtnumber04 = 24030 if districtname04 == "Laurentides--Labelle"
replace districtnumber04 = 24031 if districtname04 == "Laurier"
replace districtnumber04 = 24032 if districtname04 == "Laval"
replace districtnumber04 = 24033 if districtname04 == "Laval--Les Îles"
replace districtnumber04 = 24034 if districtname04 == "Lévis--Bellechasse"
replace districtnumber04 = 24035 if districtname04 == "Longueuil"
replace districtnumber04 = 24036 if districtname04 == "Lotbinière--Chutes-de-la-Chaudière"
replace districtnumber04 = 24037 if districtname04 == "Louis-Hébert"
replace districtnumber04 = 24038 if districtname04 == "Louis-Saint-Laurent"
replace districtnumber04 = 24039 if districtname04 == "Manicouagan"
replace districtnumber04 = 24040 if districtname04 == "Marc-Aurèle-Fortin"
replace districtnumber04 = 24041 if districtname04 == "Matapédia--Matane"
replace districtnumber04 = 24042 if districtname04 == "Mégantic--L'Érable"
replace districtnumber04 = 24043 if districtname04 == "Montcalm"
replace districtnumber04 = 24044 if districtname04 == "Mount Royal/Mont-Royal"
replace districtnumber04 = 24045 if districtname04 == "Notre-Dame-de-Grâce--Lachine"
replace districtnumber04 = 24046 if districtname04 == "Nunavik--Eeyou"
replace districtnumber04 = 24047 if districtname04 == "Outremont"
replace districtnumber04 = 24048 if districtname04 == "Papineau"
replace districtnumber04 = 24049 if districtname04 == "Pierrefonds--Dollard"
replace districtnumber04 = 24050 if districtname04 == "Pontiac"
replace districtnumber04 = 24051 if districtname04 == "Portneuf"
replace districtnumber04 = 24052 if districtname04 == "Québec"
replace districtnumber04 = 24053 if districtname04 == "Repentigny"
replace districtnumber04 = 24054 if districtname04 == "Richelieu"
replace districtnumber04 = 24055 if districtname04 == "Richmond--Arthabaska"
replace districtnumber04 = 24056 if districtname04 == "Rimouski--Témiscouata"
replace districtnumber04 = 24057 if districtname04 == "Rivière-des-Mille-Îles"
replace districtnumber04 = 24058 if districtname04 == "Rivière-du-Loup--Montmagny"
replace districtnumber04 = 24059 if districtname04 == "Rivière-du-Nord"
replace districtnumber04 = 24060 if districtname04 == "Roberval"
replace districtnumber04 = 24061 if districtname04 == "Rosemont--La Petite-Patrie"
replace districtnumber04 = 24062 if districtname04 == "Saint-Bruno--Saint-Hubert"
replace districtnumber04 = 24063 if districtname04 == "Saint-Hyacinthe--Bagot"
replace districtnumber04 = 24064 if districtname04 == "Saint-Jean"
replace districtnumber04 = 24065 if districtname04 == "Saint-Lambert"
replace districtnumber04 = 24066 if districtname04 == "Saint-Laurent--Cartierville"
replace districtnumber04 = 24067 if districtname04 == "Saint-Léonard--Saint-Michel"
replace districtnumber04 = 24068 if districtname04 == "Saint-Maurice--Champlain"
replace districtnumber04 = 24069 if districtname04 == "Shefford"
replace districtnumber04 = 24070 if districtname04 == "Sherbrooke"
replace districtnumber04 = 24071 if districtname04 == "Terrebonne--Blainville"
replace districtnumber04 = 24072 if districtname04 == "Trois-Rivières"
replace districtnumber04 = 24073 if districtname04 == "Vaudreuil-Soulanges"
replace districtnumber04 = 24074 if districtname04 == "Verchères--Les Patriotes"
replace districtnumber04 = 24075 if districtname04 == "Westmount--Ville-Marie"
replace districtnumber04 = 35001 if districtname04 == "Ajax--Pickering"
replace districtnumber04 = 35002 if districtname04 == "Algoma--Manitoulin--Kapuskasing"
replace districtnumber04 = 35003 if districtname04 == "Ancaster--Dundas--Flamborough--Westdale"
replace districtnumber04 = 35004 if districtname04 == "Barrie"
replace districtnumber04 = 35005 if districtname04 == "Beaches--East York"
replace districtnumber04 = 35006 if districtname04 == "Bramalea--Gore--Malton"
replace districtnumber04 = 35007 if districtname04 == "Brampton--Springdale"
replace districtnumber04 = 35008 if districtname04 == "Brampton West/Brampton-Ouest"
replace districtnumber04 = 35009 if districtname04 == "Brant"
replace districtnumber04 = 35010 if districtname04 == "Burlington"
replace districtnumber04 = 35011 if districtname04 == "Cambridge"
replace districtnumber04 = 35012 if districtname04 == "Carleton--Lanark"
replace districtnumber04 = 35013 if districtname04 == "Chatham-Kent--Essex"
replace districtnumber04 = 35014 if districtname04 == "Clarington--Scugog--Uxbridge"
replace districtnumber04 = 35015 if districtname04 == "Davenport"
replace districtnumber04 = 35016 if districtname04 == "Don Valley East/Don Valley-Est"
replace districtnumber04 = 35017 if districtname04 == "Don Valley West/Don Valley-Ouest"
replace districtnumber04 = 35018 if districtname04 == "Dufferin--Caledon"
replace districtnumber04 = 35019 if districtname04 == "Eglinton--Lawrence"
replace districtnumber04 = 35020 if districtname04 == "Elgin--Middlesex--London"
replace districtnumber04 = 35021 if districtname04 == "Essex"
replace districtnumber04 = 35022 if districtname04 == "Etobicoke Centre/Etobicoke-Centre"
replace districtnumber04 = 35023 if districtname04 == "Etobicoke--Lakeshore"
replace districtnumber04 = 35024 if districtname04 == "Etobicoke North/Etobicoke-Nord"
replace districtnumber04 = 35025 if districtname04 == "Glengarry--Prescott--Russell"
replace districtnumber04 = 35026 if districtname04 == "Grey--Bruce--Owen Sound"
replace districtnumber04 = 35027 if districtname04 == "Guelph"
replace districtnumber04 = 35028 if districtname04 == "Haldimand--Norfolk"
replace districtnumber04 = 35029 if districtname04 == "Haliburton--Kawartha Lakes--Brock"
replace districtnumber04 = 35030 if districtname04 == "Halton"
replace districtnumber04 = 35031 if districtname04 == "Hamilton Centre/Hamilton-Centre"
replace districtnumber04 = 35032 if districtname04 == "Hamilton East--Stoney Creek/Hamilton-Est--Stoney Creek"
replace districtnumber04 = 35033 if districtname04 == "Hamilton Mountain"
replace districtnumber04 = 35034 if districtname04 == "Huron--Bruce"
replace districtnumber04 = 35035 if districtname04 == "Kenora"
replace districtnumber04 = 35036 if districtname04 == "Kingston and the Islands/Kingston et les Îles"
replace districtnumber04 = 35037 if districtname04 == "Kitchener Centre/Kitchener-Centre"
replace districtnumber04 = 35038 if districtname04 == "Kitchener--Conestoga"
replace districtnumber04 = 35039 if districtname04 == "Kitchener--Waterloo"
replace districtnumber04 = 35040 if districtname04 == "Lanark--Frontenac--Lennox and Addington"
replace districtnumber04 = 35041 if districtname04 == "Leeds--Grenville"
replace districtnumber04 = 35042 if districtname04 == "London--Fanshawe"
replace districtnumber04 = 35043 if districtname04 == "London North Centre/London-Centre-Nord"
replace districtnumber04 = 35044 if districtname04 == "London West/London-Ouest"
replace districtnumber04 = 35045 if districtname04 == "Markham--Unionville"
replace districtnumber04 = 35046 if districtname04 == "Middlesex--Kent--Lambton"
replace districtnumber04 = 35047 if districtname04 == "Mississauga--Brampton South/Mississauga--Brampton-Sud"
replace districtnumber04 = 35048 if districtname04 == "Mississauga East--Cooksville/Mississauga-Est--Cooksville"
replace districtnumber04 = 35049 if districtname04 == "Mississauga--Erindale"
replace districtnumber04 = 35050 if districtname04 == "Mississauga South/Mississauga-Sud"
replace districtnumber04 = 35051 if districtname04 == "Mississauga--Streetsville"
replace districtnumber04 = 35052 if districtname04 == "Nepean--Carleton"
replace districtnumber04 = 35053 if districtname04 == "Newmarket--Aurora"
replace districtnumber04 = 35054 if districtname04 == "Niagara Falls"
replace districtnumber04 = 35055 if districtname04 == "Niagara West--Glanbrook/Niagara-Ouest--Glanbrook"
replace districtnumber04 = 35056 if districtname04 == "Nickel Belt"
replace districtnumber04 = 35057 if districtname04 == "Nipissing--Timiskaming"
replace districtnumber04 = 35058 if districtname04 == "Northumberland--Quinte West"
replace districtnumber04 = 35059 if districtname04 == "Oak Ridges--Markham"
replace districtnumber04 = 35060 if districtname04 == "Oakville"
replace districtnumber04 = 35061 if districtname04 == "Oshawa"
replace districtnumber04 = 35062 if districtname04 == "Ottawa Centre/Ottawa-Centre"
replace districtnumber04 = 35063 if districtname04 == "Ottawa--Orléans"
replace districtnumber04 = 35064 if districtname04 == "Ottawa South/Ottawa-Sud"
replace districtnumber04 = 35065 if districtname04 == "Ottawa--Vanier"
replace districtnumber04 = 35066 if districtname04 == "Ottawa West--Nepean/Ottawa-Ouest--Nepean"
replace districtnumber04 = 35067 if districtname04 == "Oxford"
replace districtnumber04 = 35068 if districtname04 == "Parkdale--High Park"
replace districtnumber04 = 35069 if districtname04 == "Parry Sound--Muskoka"
replace districtnumber04 = 35070 if districtname04 == "Perth--Wellington"
replace districtnumber04 = 35071 if districtname04 == "Peterborough"
replace districtnumber04 = 35072 if districtname04 == "Pickering--Scarborough East/Pickering--Scarborough-Est"
replace districtnumber04 = 35073 if districtname04 == "Prince Edward--Hastings"
replace districtnumber04 = 35074 if districtname04 == "Renfrew--Nipissing--Pembroke"
replace districtnumber04 = 35075 if districtname04 == "Richmond Hill"
replace districtnumber04 = 35076 if districtname04 == "St. Catharines"
replace districtnumber04 = 35077 if districtname04 == "St. Paul's"
replace districtnumber04 = 35078 if districtname04 == "Sarnia--Lambton"
replace districtnumber04 = 35079 if districtname04 == "Sault Ste. Marie"
replace districtnumber04 = 35080 if districtname04 == "Scarborough--Agincourt"
replace districtnumber04 = 35081 if districtname04 == "Scarborough Centre/Scarborough-Centre"
replace districtnumber04 = 35082 if districtname04 == "Scarborough--Guildwood"
replace districtnumber04 = 35083 if districtname04 == "Scarborough--Rouge River"
replace districtnumber04 = 35084 if districtname04 == "Scarborough Southwest/Scarborough-Sud-Ouest"
replace districtnumber04 = 35085 if districtname04 == "Simcoe--Grey"
replace districtnumber04 = 35086 if districtname04 == "Simcoe North/Simcoe-Nord"
replace districtnumber04 = 35087 if districtname04 == "Stormont--Dundas--South Glengarry"
replace districtnumber04 = 35088 if districtname04 == "Sudbury"
replace districtnumber04 = 35089 if districtname04 == "Thornhill"
replace districtnumber04 = 35090 if districtname04 == "Thunder Bay--Rainy River"
replace districtnumber04 = 35091 if districtname04 == "Thunder Bay--Superior North/Thunder Bay--Superior-Nord"
replace districtnumber04 = 35092 if districtname04 == "Timmins--James Bay/Timmins--Baie James"
replace districtnumber04 = 35093 if districtname04 == "Toronto Centre/Toronto-Centre"
replace districtnumber04 = 35094 if districtname04 == "Toronto--Danforth"
replace districtnumber04 = 35095 if districtname04 == "Trinity--Spadina"
replace districtnumber04 = 35096 if districtname04 == "Vaughan"
replace districtnumber04 = 35097 if districtname04 == "Welland"
replace districtnumber04 = 35098 if districtname04 == "Wellington--Halton Hills"
replace districtnumber04 = 35099 if districtname04 == "Whitby--Oshawa"
replace districtnumber04 = 35100 if districtname04 == "Willowdale"
replace districtnumber04 = 35101 if districtname04 == "Windsor--Tecumseh"
replace districtnumber04 = 35102 if districtname04 == "Windsor West/Windsor-Ouest"
replace districtnumber04 = 35103 if districtname04 == "York Centre/York-Centre"
replace districtnumber04 = 35104 if districtname04 == "York--Simcoe"
replace districtnumber04 = 35105 if districtname04 == "York South--Weston/York-Sud--Weston"
replace districtnumber04 = 35106 if districtname04 == "York West/York-Ouest"
replace districtnumber04 = 46001 if districtname04 == "Brandon--Souris"
replace districtnumber04 = 46002 if districtname04 == "Charleswood--St. James"
replace districtnumber04 = 46003 if districtname04 == "Churchill"
replace districtnumber04 = 46004 if districtname04 == "Dauphin--Swan River"
replace districtnumber04 = 46005 if districtname04 == "Elmwood--Transcona"
replace districtnumber04 = 46006 if districtname04 == "Kildonan--St. Paul"
replace districtnumber04 = 46007 if districtname04 == "Portage--Lisgar"
replace districtnumber04 = 46008 if districtname04 == "Provencher"
replace districtnumber04 = 46009 if districtname04 == "Saint Boniface/Saint-Boniface"
replace districtnumber04 = 46010 if districtname04 == "Selkirk--Interlake"
replace districtnumber04 = 46011 if districtname04 == "Winnipeg Centre/Winnipeg-Centre"
replace districtnumber04 = 46012 if districtname04 == "Winnipeg North/Winnipeg-Nord"
replace districtnumber04 = 46013 if districtname04 == "Winnipeg South/Winnipeg-Sud"
replace districtnumber04 = 46014 if districtname04 == "Winnipeg South Centre/Winnipeg-Centre-Sud"
replace districtnumber04 = 47001 if districtname04 == "Battlefords--Lloydminster"
replace districtnumber04 = 47002 if districtname04 == "Blackstrap"
replace districtnumber04 = 47003 if districtname04 == "Churchill River/Rivière Churchill"
replace districtnumber04 = 47004 if districtname04 == "Cypress Hills--Grasslands"
replace districtnumber04 = 47005 if districtname04 == "Palliser"
replace districtnumber04 = 47006 if districtname04 == "Prince Albert"
replace districtnumber04 = 47007 if districtname04 == "Regina--Lumsden--Lake Centre"
replace districtnumber04 = 47008 if districtname04 == "Regina--Qu'Appelle"
replace districtnumber04 = 47009 if districtname04 == "Saskatoon--Humboldt"
replace districtnumber04 = 47010 if districtname04 == "Saskatoon--Rosetown--Biggar"
replace districtnumber04 = 47011 if districtname04 == "Saskatoon--Wanuskewin"
replace districtnumber04 = 47012 if districtname04 == "Souris--Moose Mountain"
replace districtnumber04 = 47013 if districtname04 == "Wascana"
replace districtnumber04 = 47014 if districtname04 == "Yorkton--Melville"
replace districtnumber04 = 48001 if districtname04 == "Athabasca"
replace districtnumber04 = 48002 if districtname04 == "Calgary East/Calgary-Est"
replace districtnumber04 = 48003 if districtname04 == "Calgary North Centre/Calgary-Centre-Nord"
replace districtnumber04 = 48004 if districtname04 == "Calgary Northeast/Calgary-Nord-Est"
replace districtnumber04 = 48005 if districtname04 == "Calgary--Nose Hill"
replace districtnumber04 = 48006 if districtname04 == "Calgary South Centre/Calgary-Centre-Sud"
replace districtnumber04 = 48007 if districtname04 == "Calgary Southeast/Calgary-Sud-Est"
replace districtnumber04 = 48008 if districtname04 == "Calgary Southwest/Calgary-Sud-Ouest"
replace districtnumber04 = 48009 if districtname04 == "Calgary West/Calgary-Ouest"
replace districtnumber04 = 48010 if districtname04 == "Crowfoot"
replace districtnumber04 = 48011 if districtname04 == "Edmonton--Beaumont"
replace districtnumber04 = 48012 if districtname04 == "Edmonton Centre/Edmonton-Centre"
replace districtnumber04 = 48013 if districtname04 == "Edmonton East/Edmonton-Est"
replace districtnumber04 = 48014 if districtname04 == "Edmonton--Leduc"
replace districtnumber04 = 48015 if districtname04 == "Edmonton--St. Albert"
replace districtnumber04 = 48016 if districtname04 == "Edmonton--Sherwood Park"
replace districtnumber04 = 48017 if districtname04 == "Edmonton--Spruce Grove"
replace districtnumber04 = 48018 if districtname04 == "Edmonton--Strathcona"
replace districtnumber04 = 48019 if districtname04 == "Lethbridge"
replace districtnumber04 = 48020 if districtname04 == "Macleod"
replace districtnumber04 = 48021 if districtname04 == "Medicine Hat"
replace districtnumber04 = 48022 if districtname04 == "Peace River"
replace districtnumber04 = 48023 if districtname04 == "Red Deer"
replace districtnumber04 = 48024 if districtname04 == "Vegreville--Wainwright"
replace districtnumber04 = 48025 if districtname04 == "Westlock--St. Paul"
replace districtnumber04 = 48026 if districtname04 == "Wetaskiwin"
replace districtnumber04 = 48027 if districtname04 == "Wild Rose"
replace districtnumber04 = 48028 if districtname04 == "Yellowhead"
replace districtnumber04 = 59001 if districtname04 == "Abbotsford"
replace districtnumber04 = 59002 if districtname04 == "Burnaby--Douglas"
replace districtnumber04 = 59003 if districtname04 == "Burnaby--New Westminster"
replace districtnumber04 = 59004 if districtname04 == "Cariboo--Prince George"
replace districtnumber04 = 59005 if districtname04 == "Chilliwack--Fraser Canyon"
replace districtnumber04 = 59006 if districtname04 == "Delta--Richmond East/Delta--Richmond-Est"
replace districtnumber04 = 59007 if districtname04 == "Dewdney--Alouette"
replace districtnumber04 = 59008 if districtname04 == "Esquimalt--Juan de Fuca"
replace districtnumber04 = 59009 if districtname04 == "Fleetwood--Port Kells"
replace districtnumber04 = 59010 if districtname04 == "Kamloops--Thompson"
replace districtnumber04 = 59011 if districtname04 == "Kelowna"
replace districtnumber04 = 59012 if districtname04 == "Kootenay--Columbia"
replace districtnumber04 = 59013 if districtname04 == "Langley"
replace districtnumber04 = 59014 if districtname04 == "Nanaimo--Alberni"
replace districtnumber04 = 59015 if districtname04 == "Nanaimo--Cowichan"
replace districtnumber04 = 59016 if districtname04 == "Newton--North Delta/Newton--Delta-Nord"
replace districtnumber04 = 59017 if districtname04 == "New Westminster--Coquitlam"
replace districtnumber04 = 59018 if districtname04 == "North Okanagan--Shuswap/Okanagan-Nord--Shuswap"
replace districtnumber04 = 59019 if districtname04 == "North Vancouver"
replace districtnumber04 = 59020 if districtname04 == "Okanagan--Coquihalla"
replace districtnumber04 = 59021 if districtname04 == "Port Moody--Westwood--Port Coquitlam"
replace districtnumber04 = 59022 if districtname04 == "Prince George--Peace River"
replace districtnumber04 = 59023 if districtname04 == "Richmond"
replace districtnumber04 = 59024 if districtname04 == "Saanich--Gulf Islands"
replace districtnumber04 = 59025 if districtname04 == "Skeena--Bulkley Valley"
replace districtnumber04 = 59026 if districtname04 == "Southern Interior"
replace districtnumber04 = 59027 if districtname04 == "South Surrey--White Rock--Cloverdale/Surrey-Sud--White Rock--Cloverdale"
replace districtnumber04 = 59028 if districtname04 == "Surrey North/Surrey-Nord"
replace districtnumber04 = 59029 if districtname04 == "Vancouver Centre/Vancouver-Centre"
replace districtnumber04 = 59030 if districtname04 == "Vancouver East/Vancouver-Est"
replace districtnumber04 = 59031 if districtname04 == "Vancouver Island North/Île de Vancouver-Nord"
replace districtnumber04 = 59032 if districtname04 == "Vancouver Kingsway"
replace districtnumber04 = 59033 if districtname04 == "Vancouver Quadra"
replace districtnumber04 = 59034 if districtname04 == "Vancouver South/Vancouver-Sud"
replace districtnumber04 = 59035 if districtname04 == "Victoria"
replace districtnumber04 = 59036 if districtname04 == "West Vancouver--Sunshine Coast"
replace districtnumber04 = 60001 if districtname04 == "Yukon"
replace districtnumber04 = 61001 if districtname04 == "Western Arctic"
replace districtnumber04 = 62001 if districtname04 == "Nunavut"

* Selecting, ordering, and sorting variables

keep districtnumber04 districtname04 party04 rank04 numberofvotes04 numberofvotespercent04 margin04 enep04 winner04 runnerup04 thirdplace04

order districtnumber04 districtname04 party04 rank04 numberofvotes04 numberofvotespercent04 margin04 enep04 winner04 runnerup04 thirdplace04

sort districtnumber04 rank04

drop if rank04 > 3

* Drop duplicates

duplicates tag districtnumber04, gen(dup)
duplicates drop districtnumber04, force
drop dup

* Create election variable

gen election04 = "2004 Canadian federal election"

* Saving file

save CA04_results, replace

log close